package com.lych.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.lych.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.w3c.dom.UserDataHandler;

import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi{

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 更新位置
     * @param longitude
     * @param latitude
     * @param addrStr
     * @return
     */
    @Override
    public Boolean location(Long userId, Double longitude, Double latitude, String addrStr) {
        try {
            //查询表中是否有位置信息
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
            //判断是否存在
            if (location == null) {
                //为空，手动添加用户位置信息
                location = new UserLocation();
                location.setUserId(userId);
                location.setUpdated(System.currentTimeMillis());
                location.setLastUpdated(System.currentTimeMillis());
                location.setCreated(System.currentTimeMillis());
                location.setAddress(addrStr);
                location.setLocation(new GeoJsonPoint(longitude,latitude));
                mongoTemplate.save(location);
            }else {
                //存在，更新
                Update update = Update.update("updated",System.currentTimeMillis())
                        .set("lastUpdated",location.getUpdated())
                        .set("location",new GeoJsonPoint(longitude,latitude));
                mongoTemplate.updateFirst(query,update,UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查询当前用户附近的人
     * @param userId
     * @param valueOf
     * @return
     */
    @Override
    public List<Long> findNerUserId(Long userId, Double valueOf) {
        //通过id查询信息
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        //根据当前用户绘制原点
        GeoJsonPoint point = location.getLocation();
        //得到半径
        Distance distance = new Distance(valueOf / 1000, Metrics.KILOMETERS);
        //绘制圆形
        Circle circle = new Circle(point,distance);
        //查询
        Query location1 = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(location1, UserLocation.class);
        return CollUtil.getFieldValues(userLocations,"userId",Long.class);
    }
}
